package com.example.spring.exercise.mapper;

import com.example.spring.exercise.service.dto.rouge.BrandData;
import com.example.spring.exercise.service.dto.rouge.BrandInfo;
import com.example.spring.exercise.service.dto.rouge.RougeInfo;
import com.example.spring.exercise.service.dto.rouge.ShopInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author : zhayh
 * @date : 2021-3-31 20:11
 * @description :
 */

@Repository
public interface RougeMapper {
    @Select({"<script>",
            "select a.brand_id, b.brand_name, count(1), AVG(a.rouge_price) ",
            " from rouge_info a, brand_info b ",
            " where a.brand_id = b.id group by a.brand_id ",
            " order AVG(a.rouge_price) DESC limit 0, 5",
            " </script>"
    })
    List<BrandData> selectTopBrandData();

    @Insert("insert into rouge_info(rouge_name, rouge_price, shop_id, brand_id) " +
            "values(#{rougeName},#{rougePrice},#{rougeShop.id},#{rougeBrand.id})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insert(RougeInfo rougeInfo);

    @Select("select * from brand_info where id=#{id}")
    BrandInfo getBrandById(Long id);

    @Select("select * from shop_info where id=#{id}")
    ShopInfo getShopById(Long id);

    @Select("select count(1) from rouge_info")
    Integer selectCount();

    @Select({"select a.* from rouge_info a ",
            " left join brand_info b on b.id=a.brand_id ",
            " left join shop_info c on c.id=a.shop_id ",
            " where a.rougeBrand.brandCountry = #{country} ",
            " and a.rougePrice >= ${minPrice} ",
            " and a.rougePrice <= ${maxPrice}}"
    })
    @Results({
            @Result(column = "shop_id", property = "shopInfo",
                    one = @One(select = "getShopById", fetchType = FetchType.EAGER)),
            @Result(column = "brand_id", property = "brandInfo",
                    one = @One(select = "getBrandById", fetchType = FetchType.EAGER))
    })
    List<RougeInfo> selectByCountry(@Param("country") String country,
                                    @Param("minPrice") Float minPrice,
                                    @Param("maxPrice") Float maxPrice,
                                    @Param("pageNum") int pageNum,
                                    @Param("pageSize") int pageSize);
}
